package view
{
	import flash.events.Event;
	
	import model.AnneeEtude;
	import model.AnneeUniversitaire;
	import model.Enseignant;
	import model.Formation;
	import model.proxy.AnneeEtudeProxy;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import view.components.AnneeEtudePanel;
	
	public class AnneeEtudePanelMediator extends Mediator implements IMediator 
	{ 
		
		public static const NAME : String = 'AnneeEtudePanelMediator'; 
			
		/** 
		 * Constructor 
		 * Listen event of the component to send notification 
		 */ 
		 public function AnneeEtudePanelMediator(viewComponent : AnneeEtudePanel) 
		 { 
		 	super(NAME, viewComponent); 
		 	viewComponent.addEventListener(AnneeEtudePanel.OBTENIR_LISTE_ANNEES_ETUDE, obtenirListeAnneesEtudeNotification);
		 	viewComponent.addEventListener(AnneeEtudePanel.OBTENIR_LISTE_ANNEES_UNIVERSITAIRE_ETUDE, obtenirListeAnneesUniversitaireNotification);
		 	viewComponent.addEventListener(AnneeEtudePanel.OBTENIR_LISTE_ENSEIGNANTS_ETUDE, obtenirListeEnseignantsNotification);
		 	viewComponent.addEventListener(AnneeEtudePanel.OBTENIR_LISTE_FORMATIONS_ETUDE, obtenirListeFormationsNotification);
		 	viewComponent.addEventListener(AnneeEtudePanel.VALIDER_AJOUT_ANNEES_ETUDE, validerAjoutAnneesEtudeNotification);
		 	viewComponent.addEventListener(AnneeEtudePanel.VALIDER_MODIF_ANNEES_ETUDE, validerModifAnneesEtudeNotification);
		 	viewComponent.addEventListener(AnneeEtudePanel.SUPPRIMER_ANNEES_ETUDE, supprimerAnneeEtudeNotification);
		 } 
		 
		 private function obtenirListeAnneesEtudeNotification(event : Event=null):void
		 { 
		 	 var formation:Formation = anneeEtudePanel.comboFormation.selectedItem.valueOf();
		 	 var anneeUniversitaire:AnneeUniversitaire = anneeEtudePanel.comboAnneeUniversitaire.selectedItem.valueOf();
			 sendNotification(AnneeEtudePanel.OBTENIR_LISTE_ANNEES_ETUDE, [anneeUniversitaire,formation]);
		 }
		
		 private function obtenirListeAnneesUniversitaireNotification(event : Event=null):void
		 { 
			 sendNotification(AnneeEtudePanel.OBTENIR_LISTE_ANNEES_UNIVERSITAIRE_ETUDE, null);
		 }
		 
		 private function obtenirListeFormationsNotification(event : Event=null):void
		 { 
			 sendNotification(AnneeEtudePanel.OBTENIR_LISTE_FORMATIONS_ETUDE, null);
		 }
		 
		 private function obtenirListeEnseignantsNotification(event : Event=null):void
		 { 
			 var formation:Formation = anneeEtudePanel.comboFormation.selectedItem.valueOf();
		 	 var anneeUniversitaire:AnneeUniversitaire = anneeEtudePanel.comboAnneeUniversitaire.selectedItem.valueOf();
			 sendNotification(AnneeEtudePanel.OBTENIR_LISTE_ENSEIGNANTS_ETUDE, [anneeUniversitaire,formation]);
		 }
		 
		 private function validerAjoutAnneesEtudeNotification(event : Event=null):void
		 {
		 	 var anneeEtude:AnneeEtude = new AnneeEtude(); 
			 anneeEtude.nom = anneeEtudePanel.nom.text;
			 anneeEtude.duree_seance = parseInt(anneeEtudePanel.duree_seance.text);
			 anneeEtude.desc_objectifs = anneeEtudePanel.desc_objectifs.text;
			 anneeEtude.nb_seance_apres_midi = parseInt(anneeEtudePanel.nb_seance_apres_midi.text);
			 anneeEtude.nb_seance_matin = parseInt(anneeEtudePanel.nb_seance_matin.text);
			 anneeEtude.responsable = anneeEtudePanel.responsable.selectedItem.valueOf().id_enseignant;
			 anneeEtude.id_an_univ = anneeEtudePanel.comboAnneeUniversitaire.selectedItem.valueOf().id_an_univ;
			 var formation:Formation = new Formation();
			 formation = anneeEtudePanel.comboFormation.selectedItem.valueOf();
			 var responsable:Enseignant = new Enseignant();
			 responsable = anneeEtudePanel.responsable.selectedItem.valueOf();
			 sendNotification(AnneeEtudePanel.VALIDER_AJOUT_ANNEES_ETUDE, [anneeEtude, formation, responsable]); 
		 }
		 
		 private function validerModifAnneesEtudeNotification(event : Event=null):void
		 {
		 	 var anneeEtude:AnneeEtude = anneeEtudePanel.affichage.selectedItem.valueOf(); 
			 anneeEtude.nom = anneeEtudePanel.nom.text;
			 anneeEtude.duree_seance = parseInt(anneeEtudePanel.duree_seance.text);
			 anneeEtude.desc_objectifs = anneeEtudePanel.desc_objectifs.text;
			 anneeEtude.nb_seance_apres_midi = parseInt(anneeEtudePanel.nb_seance_apres_midi.text);
			 anneeEtude.nb_seance_matin = parseInt(anneeEtudePanel.nb_seance_matin.text);
			 anneeEtude.responsable = anneeEtudePanel.responsable.selectedItem.valueOf().id_enseignant;
			 var formation:Formation = new Formation();
			 formation = anneeEtudePanel.comboFormation.selectedItem.valueOf();
			 sendNotification(AnneeEtudePanel.VALIDER_MODIF_ANNEES_ETUDE, [anneeEtude,formation]); 
		 }
		 
		 private function supprimerAnneeEtudeNotification(event : Event=null):void
		 {
		 	var anneeEtude:AnneeEtude = anneeEtudePanel.affichage.selectedItem.valueOf();
		 	var formation:Formation = anneeEtudePanel.comboFormation.selectedItem.valueOf();
		 	var anneeUniversitaire:AnneeUniversitaire = anneeEtudePanel.comboAnneeUniversitaire.selectedItem.valueOf();
		 	sendNotification(AnneeEtudePanel.SUPPRIMER_ANNEES_ETUDE, [anneeEtude, formation, anneeUniversitaire]);
		 }
		
		
		override public function listNotificationInterests():Array 
		 { 
		 	return [ AnneeEtudeProxy.LISTE_ANNEES_ETUDE_OBTENUE,
		 			AnneeEtudeProxy.LISTE_ANNEES_UNIVERSITAIRE_ETUDE_OBTENUE,
		 			AnneeEtudeProxy.LISTE_FORMATIONS_OBTENUE,
		 			AnneeEtudeProxy.LISTE_ENSEIGNANTS_OBTENUE
		 			]; 
		 } 
		 

		 override public function handleNotification(note : INotification):void 
		 { 
			 switch (note.getName()) 
			 { 
			 	case AnneeEtudeProxy.LISTE_ANNEES_ETUDE_OBTENUE:
			 	{
			 		anneeEtudePanel.affichage.dataProvider = (note.getBody() as Array);	
			 		break;
			 	}
			 	case AnneeEtudeProxy.LISTE_ANNEES_UNIVERSITAIRE_ETUDE_OBTENUE:
			 	{
			 		anneeEtudePanel.comboAnneeUniversitaire.dataProvider = (note.getBody() as Array);
			 		break;
			 	} 
			 	case AnneeEtudeProxy.LISTE_FORMATIONS_OBTENUE:
			 	{
			 		anneeEtudePanel.comboFormation.dataProvider = (note.getBody() as Array);
			 		anneeEtudePanel.choixCombo();
			 		break;
			 	} 
			 	case AnneeEtudeProxy.LISTE_ENSEIGNANTS_OBTENUE:
			 	{
			 		anneeEtudePanel.responsable.dataProvider = (note.getBody() as Array);
			 		break;
			 	} 
			 	default:
			 	break;
		 	} 
		 }
		 
		 
		private function get anneeEtudePanel():AnneeEtudePanel 
		 { 
		 	return viewComponent as AnneeEtudePanel; 
		 }
	}	 	
		 
}